import Vue from 'vue'
import store from '../../../../../store'
export default {
    //分页获取官网大师列表
    /*
    * obj:{
    *   路径参数：
        storeId: 102                                           ----店铺id
        menuId： 1                                             ----菜单id
        classId: 1                                             ----类目id(-1 默认查询全部)
        pageSize: 10                                           ----分页大小
        pageNum:1                                              ----页码
        success:function                                       ----成功的回调
        fail:function                                          ----失败的回调
        failText:function                                      ----失败的回调
    * }
    * 结果
    *  state.master
    * */
    masterListApi (state, obj) {
        if (!obj) {
            return
        }
        let storeId = obj.storeId || 0;
        let menuId = obj.menuId || 0;
        let classId = obj && (obj.classId || obj.classId == 0) ? obj.classId : -1;
        let pageSize = obj.pageSize || 5;
        let pageNum = obj.pageNum || 1;
        let userId = obj.userId || 0;
        let headMasterId = obj.shareId || 0
        let url = `${Vue.prototype.NW_API_API_LOCALE_VERSION}/big/master/query/store/${storeId}/menu/${menuId}/${classId}?headMasterId=${headMasterId}&pageSize=${pageSize}&pageNum=${pageNum}&userId=${userId}`;
        Vue.prototype.xurl.getSync(url).then(data => {
            if (data.success) {
                if (data) {
                    state.master.listObj = Object.assign({}, state.master.listObj, data);
                    let tempObj = Object.assign({}, { storeId, menuId }, state.master.listObj);
                    if (!obj.showType || obj.showType == 'detail') {
                        store.commit('masterBoxsDeal', tempObj);
                    } else if (obj.showType == 'classify') {
                        store.commit('masterBoxsClassify', tempObj);
                    }
                    if (obj && obj.successFn) {
                        obj.successFn();
                    }
                }
            } else if (data.errMsg) {
                if (obj && obj.fail && typeof obj.fail == 'function') {
                    obj.fail(data);
                } else if (obj.toast) {
                    Vue.$toast({ message: data.errMsg });
                } else {
                    console.log(data.errMsg);
                }
            } else {
                if (obj.failText) {
                    Vue.$toast({ message: obj.failText });
                }
            }
        })
    },
    //获取大师详情
    /*
    * obj:{
    *   bigMasterId:0,//大师Id
    *   storeId:0,//店铺Id
    * }
    * */
    masterDetail (state, obj) {
        if (!obj) {
            return
        }
        let storeId = obj.storeId || 0;
        let bigMasterId = obj.id || 0;
        let url = `${Vue.prototype.NW_API_API_LOCALE_VERSION}/big/master/getById/${storeId}/${bigMasterId}`;
        Vue.prototype.xurl.getSync(url).then(data => {
            if (data.success) {
                if (data.result) {
                    state.master.detailObj = Object.assign({}, state.master.detailObj, data.result);
                }
            } else if (data.errMsg) {
                Vue.$toast({ message: data.errMsg });
            } else {
                if (obj.failText) {
                    Vue.$toast({ message: obj.failText });
                }
            }
        })
    },
    /*作品集合*/
    /*
    * obj:{
    *   bigMasterId:0,//大师Id
    *   pageSize:10,//每次获取条数
    *   pageNum:1,//分页获取
    * }
    *
    * */
    masterWorks (state, obj) {
        if (!obj) {
            return
        }
        let bigMasterId = obj.id || 0;
        let pageSize = obj.pageSize || 5;
        let pageNum = obj.pageNum || 1;
        let loadingMore = obj.loadingMore || false;
        let url = `${Vue.prototype.NW_API_API_LOCALE_VERSION}/big/master/query/bigMaster/${bigMasterId}/items/detail?pageSize=${pageSize}&pageNum=${pageNum}`;
        Vue.prototype.xurl.getSync(url).then(data => {
            if (data.success) {
                if (Array.isArray(data.list) && data.list.length > 0) {
                    state.master.workListObj = Object.assign({}, state.master.workListObj, { loadingMore }, data);
                    store.commit('masterWorksDeal', state.master.workListObj);
                }
            } else if (data.errMsg) {
                Vue.$toast({ message: data.errMsg });
            } else {
                if (obj.failText) {
                    Vue.$toast({ message: obj.failText });
                }
            }
        })
    },
    /*分类*/
    /*
    * obj:{
    *   storeId：102                                                         //----店铺id
    *   menuId:                                                              //----菜单id
    * }
    *
    * */
    marsterClassify (state, obj) {
        if (!obj) {
            return
        }
        let storeId = obj.storeId || 0;
        let menuId = obj.menuId || 1;
        let url = `${Vue.prototype.NW_API_API_LOCALE_VERSION}/big/master/store/${storeId}/menu/${menuId}/list`;
        Vue.prototype.xurl.getSync(url).then(data => {
            if (data.success) {
                if (Array.isArray(data.list) && data.list.length > 0) {
                    state.master.classifyMenuList = data.list;
                }
            } else if (data.errMsg) {
                Vue.$toast({ message: data.errMsg });
            } else {
                if (obj.failText) {
                    Vue.$toast({ message: obj.failText });
                }
            }
        })
    }
}